package mvv.learn.spring.controller;

import java.util.List;

import mvv.learn.spring.dto.elasticsearch.ElsEmployeeDto;
import mvv.learn.spring.dto.response.ResResponse;
import mvv.learn.spring.services.accessdata.EmployeeService;
import mvv.learn.spring.services.elastic.TestElasticsearchService;
import mvv.learn.spring.utitilies.AppParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Manh Vu
 */
@Controller
@RequestMapping(value = "/test")
public class TestController extends AbsController {
	private static final Logger log = LoggerFactory.getLogger(TestController.class);

	@Autowired
	private TestElasticsearchService elasticsearchService;

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/elasticsearch/all", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResResponse testElasticsearch() {
		ResResponse res = null;
		res = elasticsearchService.searchAll();
		return res;
	}

	@RequestMapping(value = "/getAllEmployees", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResResponse getAllEmployees(@RequestParam(required = false) Long employeeId) {
		ResResponse res = null;

		try {
			List<ElsEmployeeDto> list = employeeService.getElsEmployeeDtos(employeeId);
			res = new ResResponse(AppParam.VAL_GOOD, null, list);
		}catch (Exception e) {
			log.error(e.getMessage(), e);
			res = new ResResponse(AppParam.VAL_BAD, null, e);
		}

		return res;
	}
}
